<?php


namespace App\Console\Commands;


use App\Modules\Order\Models\Order;
use App\Modules\User\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class MinutePush30 extends Command
{
    /**
     * console命令的名称和签名。
     *
     * @var string
     */
    protected $signature = 'MinutePush30';

    /**
     * console命令说明。
     *
     * @var string
     */
    protected $description = '用于下单30分钟后未支付的订单的支付通知';

    /**
     * 创建一个新的命令实例。
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *  临洮：订单超时未支付
     *  模板ID：ufJlNLyyoeMyqbRlyYRb40RtbCfOejqrkzUUQqTBu8w
     *  格式：
     * {{first.DATA}} //订单超时未评价
     * 订单号：{{keyword1.DATA}}
     * 创建时间：{{keyword2.DATA}}
     * 商品明细：{{keyword3.DATA}}
     * {{remark.DATA}} //请您及时付款以免过期。
     */
    public function handle()
    {
        $yesterday = date("Y-m-d 00:00:00", strtotime("-1 day"));
        $current = date("Y-m-d H:i:s");
        $order = Order::query()
            ->where([["created_at", '>', $yesterday], ["status", Order::STATUS_INIT], ["halfhour_remind", 0]])
            ->get();
        foreach ($order as $o) {
            $MinutesLater30 = date('Y-m-d H-i-s', strtotime("+30 minutes", strtotime($o->created_at)));


            if ($MinutesLater30 <= $current) {
                $user = User::query()->where("id", $o->user_id)->first();
                if ($user) {
                    $sendData = [$user->openid, $o->id, date("Y年m月d日 H时i分s秒"), $o->title];
                    if ($o->delivery == 0 && $o->is_prepay == 1) { //到店自取-定金支付
                        sendMessage(6, $sendData);
                    } elseif ($o->delivery > 0 && $o->is_prepay == 1) {//物流发货-定金支付
                        sendMessage(7, $sendData);
                    }
                    Log::channel('console')->info("30push", $sendData);
                    Order::query()->where("id", $o->id)->update(["halfhour_remind" => 1]);
                }

            }
        }
    }
}
